extends ./layout.jade

block title
	=topic.title

block head
	link(rel='stylesheet',href='/lib/simditor/styles/simditor.css')
	link(rel='stylesheet',href='/lib/simditor-mention/styles/simditor-mention.css')
	link(rel='stylesheet',href='/lib/highlight/src/styles/darkula.css')
	//-link(rel='stylesheet',href='/lib/highlight/src/styles/default.css')

block sidebar
	- var author=topic.author
	.ui.card
		//-.image: img(src=author.avatarUrl)
		.content
			.header
				img.mr5.avatar(src=author.avatarUrl)
				=author.loginName
			.meta: span.date=author.createAt.getFullYear()+'加入'
			.description=author.description||'这个人很懒，什么都没写。'
		.extra.content
			a
				i.write.icon
				=author.topicCount+'篇主题，'+author.replyCount+'篇回复'
		.ui.bottom.attached.button
			i.add.icon
			='添加好友'

block nav
	a.active.section(href='/tab/'+topic.tabId)=tabs[topic.tabId].name
	.divider /
	a.active.section=topic.title

block body
	.ui.basic.segment
		.ui.dividing.header=topic.title
		p
			i.user.icon
			=topic.author.loginName
			!='&nbsp;&nbsp;&nbsp;&nbsp;'
			i.wait.icon
			=topic.createAtformatted
		div.editor-style
			!=topic.content
	.ui.basic.segment
		.ui.comments
			.ui.dividing.header 评论
			each reply in replies
				.comment
					a.avatar: img(src=reply.author.avatarUrl)
					.content
						a.author=reply.author.loginName
						.metadata
							span.date=reply.createAtFormatted
						.text.editor-style!=reply.content
						.actions
							a.reply 回复
						//-.coments
		.ui.basic.center.aligned.segment
			.ui.borderless.pagination.menu
				include ./include/pagination.jade
				+pagination('/topic/'+topic._id+'/{page}',page,pageTotal)
		- if(locals.user)
			.ui.dividing.header 发表评论
			.ui.form
				form#form-post
					input(type='hidden',value=topic._id,name='topicId',required)
					.field
						textarea#editor(name='content',style="max-height:400px;")
					.ui.error.message#msg-post
					button.ui.blue.submit.labeled.icon.button 发表
						i.signup.icon

block script
	script(src='/lib/highlight/src/highlight.js')
	script.
		hljs.initHighlightingOnLoad();
		// $('pre code').each(function(i, block) {
		// 	console.log(block);
		// 	hljs.highlightBlock(block);
		// });
	- if(locals.user)
		script(src='/lib/simple-module/lib/module.js')
		script(src='/lib/simple-hotkeys/lib/hotkeys.js')
		script(src='/lib/simditor/lib/simditor.js')
		script(src='/lib/simditor-mention/lib/simditor-mention.js')

		script.
			var editor=new Simditor({
				textarea:$('#editor'),
				mention:{
					url:'/mentionedUsers/#{topic._id}',
					linkRenderer:function($linkEl,data){
						$linkEl.attr('data-uid',data.id);
					}
				}
			});
	
		include ./include/validate.jade
		script.
			//发表评论
			$('#form-post').validateInit({
				submitHandler:function(form){
					$('#form-post .form').addClass('loading');
					$.post('/reply/create',$('#form-post').serializeJson(),function(data){
						if(data.result=='success')
							window.location.reload();
						else
							$('#msg-post').text(data.msg).show();
						$('#form-post .form').removeClass('loading');
					},'json');
				}
			});